************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates main paper, Figure 4 (Expert Predictions vs. RCT Results)
* Last edited: 07 May 2021
************************************************
clear all
set more off

	use "$d/survey_of_experts.dta", clear

* Flip sign of physical health
replace bp_norm = - bp_norm
	
************
* SURVEY OF EXPERTS FINAL IMAGE
************

preserve

foreach var in correct_entries_norm labor_supply_norm savings_norm pb_ate attention_pvt_index bp_norm{ // mistakes_norm 
	* The median and quartiles are easiest:
	egen med_`var' = median(`var') //
	egen upq_`var' = pctile(`var'), p(75) //
	egen loq_`var' = pctile(`var'), p(25) //
	egen mean_`var' = mean(`var') //
	egen iqr_`var' = iqr(`var') //,
	* For upper limits of whiskers:
	egen upper_`var' = pctile(`var'), p(90)
	* For lower limits of whiskers:
	egen lower_`var' = pctile(`var'), p(10)
}

forval i = 1/6{
	gen x`i'= `i'
}

gen legend_x = 0.75
gen legend_y = 0.35
local legend_text_x = 1.35
local legend_y = 0.35


* Actual values from treatment

gen actual_value_prod = 0.02
gen actual_value_ls = -0.08
gen actual_value_correct_entries = -0.04
gen actual_value_pb = 0.007
gen actual_value_savings = -9.1/325.7
gen actual_value_pvt = 0.0146471
gen actual_value_bp = 0.02

foreach var in correct_entries_norm labor_supply_norm savings_norm pb_ate attention_pvt_index bp_norm{ //  mistakes_norm 
sum `var'
	local N_`var' = r(N)
}

* Boxplots and scatterplot in the same figure:
twoway /// 
///1st point - Correct Entries
(rspike loq_correct_entries_norm upq_correct_entries_norm x1 , blc("62 97 128") msize(*1) lwidth(vthin) ) ///
(rcap loq_correct_entries_norm upq_correct_entries_norm x1 , blc("62 97 128") msize(*1) ) ///
(scatter actual_value_correct_entries x1, mcolor(black) msymbol(diamond) msize(*1.25) ) ///
(scatter med_correct_entries_norm x1, mcolor("62 97 128") msymbol(X) msize(*2) ) ///
///
///	2nd point - Labor Supply
///
(rspike loq_labor_supply_norm upq_labor_supply_norm x2 , blc("62 97 128") msize(*1) lwidth(vthin) ) ///
(rcap loq_labor_supply_norm upq_labor_supply_norm x2 , blc("62 97 128") msize(*1) ) ///
(scatter actual_value_ls x2, mcolor(black) msymbol(diamond) msize(*1.25) ) ///
(scatter med_labor_supply_norm x2, mcolor("62 97 128") msymbol(X) msize(*2) ) ///
///
/// 3th point - BP
///
(rspike loq_bp_norm upq_bp_norm x3 , blc("62 97 128") msize(*1) lwidth(vthin) ) ///
(rcap loq_bp_norm upq_bp_norm x3 , blc("62 97 128") msize(*1) ) ///
(scatter actual_value_bp x3, mcolor(black) msymbol(diamond) msize(*1.25) ) ///
(scatter med_bp_norm x3, mcolor("62 97 128") msymbol(X) msize(*2) ) ///
///
/// 4th point - PVT
///
(rspike loq_attention_pvt_index upq_attention_pvt_index x4 , blc("62 97 128") msize(*1) lwidth(vthin) ) ///
(rcap loq_attention_pvt_index upq_attention_pvt_index x4 , blc("62 97 128") msize(*1) ) ///
(scatter actual_value_pvt x4, mcolor(black) msymbol(diamond) msize(*1.25) ) ///
(scatter med_attention_pvt_index x4, mcolor("62 97 128") msymbol(X) msize(*2) ) ///
///
/// 5th point - Savings
///
(rspike loq_savings_norm upq_savings_norm x5 , blc("62 97 128") msize(*1) lwidth(vthin) ) ///
(rcap loq_savings_norm upq_savings_norm x5 , blc("62 97 128") msize(*1) ) ///
(scatter actual_value_savings x5, mcolor(black) msymbol(diamond) msize(*1.25) ) ///
(scatter med_savings_norm x5, mcolor("62 97 128") msymbol(X) msize(*2) ) ///
///
/// 6th point - Present Bias
///
(rspike loq_pb_ate upq_pb_ate x6 , blc("62 97 128") msize(*1) lwidth(vthin) ) ///
(rcap loq_pb_ate upq_pb_ate x6 , blc("62 97 128") msize(*1) ) ///
(scatter actual_value_pb x6, mcolor(black) msymbol(diamond) msize(*1.25) ) ///
(scatter med_pb_ate x6, mcolor("62 97 128") msymbol(X) msize(*2) ) ///
///
///Legends
///
(scatter legend_y legend_x, msymbol(diamond) mcolor(black)) ///
///Options
 , ytitle(Standardized Treatment Effect, height(6)) ysize(1) xsize(2) /// General plot controls
 graphregion(fcolor(white)) legend(off) xsc(r(0.5 6.65)) ylab(-0.1(0.1)0.4) ///
 xlabel(1 `""{bf:Correct Entries}"  "(N = `N_correct_entries_norm')" "' 2 `""{bf:Labor Supply}" "(N = `N_labor_supply_norm')""' ///
 3 `""{bf:Physical Health}" "(N = `N_bp_norm')""'  4 `""{bf:Attention}" "(N = `N_attention_pvt_index')""' ///
 5 `""{bf:Savings}" "(N = `N_savings_norm')""'  6 `""{bf:Present Bias}*" "(N = `N_pb_ate')""' , labsize(small)) ///
 text( 0.35 1.48  "= Actual Point Estimates") yline(0, lcolor(black) lwidth(0.2) lpatter(dash)) ///
 text( 0.32 1.31 "X = Median Prediction") yline(0, lcolor(black) lwidth(0.2) lpatter(dash))
 
graph export "$om/Figures/Figure4_survey_experts.eps", replace
graph export "$om/Figures/Figure4survey_experts.png", replace

restore
